package com.lyy.lottery.controller;

import com.lyy.lottery.common.pojo.CommonResult;
import com.lyy.lottery.common.util.JacksonUtil;
import com.lyy.lottery.controller.param.DrawPrizeParam;
import com.lyy.lottery.controller.param.ShowWinningRecordsParm;
import com.lyy.lottery.controller.result.WinningRecordResult;
import com.lyy.lottery.service.DrawPrizeService;
import com.lyy.lottery.service.dto.WinningRecordDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
public class DrawPrizeController {
    @Autowired
    private DrawPrizeService drawPrizeService;

    @RequestMapping("/draw-prize")
    public CommonResult<Boolean> drawPrize(@Validated @RequestBody DrawPrizeParam param){
        log.info("drawPrize DrawPrizeParam：{}",param);
        drawPrizeService.drawPrize(param);
        return CommonResult.sucess(true);
    }

    @RequestMapping("/winning-records/show")
    public CommonResult<List<WinningRecordResult>> showWinningReocords(@Validated @RequestBody ShowWinningRecordsParm param){
        log.info("showWinningReocords ShowWinningRecordsParm:{}", JacksonUtil.writeValueAsString(param));
        List<WinningRecordDTO> wrDTOList=drawPrizeService.getRecords(param);
        return CommonResult.sucess(converToWinningRecordResultList(wrDTOList));
    }

    private List<WinningRecordResult> converToWinningRecordResultList(List<WinningRecordDTO> wrDTOList) {
        if (CollectionUtils.isEmpty(wrDTOList)) return Arrays.asList();
        return wrDTOList.stream()
                .map(wrDTO ->{
                    WinningRecordResult result=new WinningRecordResult();
                    result.setWinnerId(wrDTO.getWinnerId());
                    result.setWinnerName(wrDTO.getWinnerName());
                    result.setPrizeName(wrDTO.getPrizeName());
                    result.setPrizeTier(wrDTO.getPrizeTier().getMessage());
                    result.setWinningTime(wrDTO.getWinnerTime());
                    return result;
                }).collect(Collectors.toList());
    }


}



















